import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text, ScrollView, Block } from '@tarojs/components'
import { Tabbar, DataLoading, MoreFormid, ResumerList, EmptyStatus } from './../../../component/index'


import './resumer.less'
import allRequest from '../../../api/allRequest';
import allExtrance from '../../../config/allExtrance';

let _this

class Resumer extends Component {

    config = {
        navigationBarTitleText: '找人才',
        // enablePullDownRefresh: Taro.getEnv() === Taro.ENV_TYPE.WEB?false:true,
    }

    constructor() {
        super();
        Taro.setNavigationBarColor({
            frontColor: Taro.getStorageSync('secondColor'),
            backgroundColor: Taro.getStorageSync('baseColor')
        })
        this.state = {
            filter: '',
            positionList: [],
            resumerList: [],
            page: 0,
            noMoreData: false,
            changeLoad: true
        }
    }

    componentWillReceiveProps(nextProps) {
        console.log(this.props, nextProps)
    }

    componentDidMount() {
        _this = this
        this.getResumePool()
    }

    componentWillUnmount() { }

    componentDidShow() { }

    componentDidHide() { }

    // onPullDownRefresh() {
    //     this.setState({
    //         page: 0,
    //     }, () => {
    //         this.getResumePool()
    //     })

    //     Taro.stopPullDownRefresh()
    // }

    refresh() {
        _this.setState({
            page: 0,
            noMoreData: false
        }, () => {
            _this.getResumePool()
        })

        Taro.stopPullDownRefresh()
    }

    getMore() {
        if (!this.state.noMoreData) {
            _this.getResumePool()
        }
    }

    //获取简历池分类
    getResumePoolCategory() {
        allRequest.resumePoolCategory().then((result) => {
            if (result) {
                this.setState({
                    positionList: result,
                })
            }
        })
    }

    //获取简历
    getResumePool() {
        let { page, filter, resumerList, changeLoad } = this.state
        let data = {
            page: ++page,
            positionId: filter
        }
        if (!changeLoad) {
            allExtrance.util.Taro.showLoading('加载中')
        }
        allRequest.resumePool(data).then((result) => {
            this.getResumePoolCategory()
            if (result) {
                if (page == 1) {
                    resumerList = result.workerPool
                } else {
                    for (let i = 0; i < result.workerPool.length; i++) {
                        resumerList.push(result.workerPool[i])
                    }
                }
                if (result.workerPool.length == 0) {
                    this.setState({ noMoreData: true })
                }
                this.setState({
                    resumerList,
                    page,
                }, () => {
                    Taro.hideLoading()
                    setTimeout(() => { this.setState({ changeLoad: false }) }, 100)
                })
            }
        }).catch((err) => {
            allExtrance.util.Taro.showToast(err)
        });
    }

    navTap(filter, e) {
        this.setState({
            filter,
            resumerList: [],
            page: 0
        }, () => {
            this.getResumePool()
        })
    }

    render() {
        const currentPage = 'businessResumeAll'
        let { positionList, resumerList, changeLoad } = this.state
        return (
            <View className='resumer'>
                <View className='filterBlock'>
                    <ScrollView className='filterPos' scrollX>
                        <View className={`item ${this.state.filter == '' ? 'current' : ''}`} onClick={this.navTap.bind(this, '')}>
                            <View className='con'>全部</View>
                            {this.state.filter == '' && <View className='xiabiao' style={`background-color:${Taro.getStorageSync('baseColor')}`}></View>}
                        </View>
                        {
                            positionList.map((item, index) =>
                                <View className={`item ${this.state.filter == item.position_id ? 'current' : ''}`} onClick={this.navTap.bind(this, item.position_id)}>
                                    <View className='con'>
                                        {item.position_title}
                                    </View>
                                    {this.state.filter == item.position_id && <View className='xiabiao' style={`background-color:${Taro.getStorageSync('baseColor')}`}></View>}
                                </View>
                            )
                        }
                    </ScrollView>
                    {/* <View className='filterAll'>筛选</View> */}
                </View>
                <ScrollView style='margin-top:51px;height:92vh' scrollY onScrollToUpper={this.refresh} upperThreshold='50' onScrollToLower={this.getMore}>
                    {resumerList.length > 0 ?
                        <MoreFormid>
                            <ResumerList resumerListAll={resumerList}></ResumerList>
                        </MoreFormid> :
                        <EmptyStatus witchPage='resumer'></EmptyStatus>
                    }


                </ScrollView>
                <Tabbar currentPage={currentPage}></Tabbar>
                {changeLoad && <DataLoading></DataLoading>}
            </View>
        )
    }
}

export default Resumer
